﻿using AutoMapper;
using MediatR;
using SmartProduction.Domain.客户管理;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Write.API.Application.Commands.SalesOrderDetailsCommand;

namespace SmartProduction.Write.API.Application.CommandHandlers.SalesOrderDetailsHandler
{
    /// <summary>
    /// 销售订单明细新增命令处理
    /// </summary>
    public class SalesOrderDetailsCreateCommandHandler : IRequestHandler<SalesOrderDetailsCreateCommand, APIResult<object>>
    {
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 销售订单明细仓储
        /// </summary>
        private readonly IBaseRepository<SalesOrderDetails> salesDetailsRepository;

        /// <summary>
        /// 销售订货仓储
        /// </summary>
        private readonly IBaseRepository<SalesOrder> salesRepository;

        public SalesOrderDetailsCreateCommandHandler(IMapper mapper,IBaseRepository<SalesOrderDetails> salesDetailsRepository,IBaseRepository<SalesOrder> salesRepository)
        {
            this.mapper = mapper;
            this.salesDetailsRepository = salesDetailsRepository;
            this.salesRepository = salesRepository;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns></returns>
        public Task<APIResult<object>> Handle(SalesOrderDetailsCreateCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<object> result = new APIResult<object>();
                SalesOrderDetailsCreateCommand Sales = new SalesOrderDetailsCreateCommand();


                DateTime date = DateTime.Now;
                string dateString = date.ToString("yyyyMMdd");
                string bianma = dateString; 
                string newCode = "XS" + bianma;

                Sales.MaterialCode = newCode;
                Sales.AllPrice = request.AllPrice;
                Sales.Price = request.Price;
                Sales.MaterialName = request.MaterialName;
                Sales.Price = request.Price;
                Sales.Num = request.Num;
                Sales.AmountPrice = request.AmountPrice;
                Sales.ReportRows = request.ReportRows;
                Sales.CreateName = "admin";
                Sales.CreateDate = date;
                Sales.MaterialId = request.MaterialId;
                Sales.MaterialName = request.MaterialName;

                var obj = mapper.Map<SalesOrder>(request);

                var sal = salesRepository.AddAsync(obj).Result;
                if (sal > 0)
                {
                    request.SalesOrderDetailss.ForEach(o =>
                    {
                        o.MaterialCode = newCode;
                        o.MaterialId = request.SalesOrderDetailsId;
                    });
                    var code = salesDetailsRepository.AddRanges(request.SalesOrderDetailss).Result;
                    if (code > 0)
                    {
                        result.Code = APIEnums.添加成功;
                        result.Msg = APIEnums.添加成功.ToString();
                        result.data = date;
                    }
                    else
                    {

                        result.Code = APIEnums.添加失败;
                        result.Msg = APIEnums.添加失败.ToString();
                    }

                }
                else
                {
                    result.Code = APIEnums.添加失败;
                    result.Msg = APIEnums.添加失败.ToString();
                }

                return Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
